home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / ListSpinner.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  3.9 KB  |  151 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.util.Enumeration;
  5. import java.util.Vector;
  6.  
  7.  
  8. /**
  9.  * Creates a text box, containing a list of item, with up and down arrows. 
  10.  * Use this component to allow your users to move through a set of fixed 
  11.  * values or type a valid value in the box.
  12.  * <p>
  13.  * At run time, only the selected value is displayed in the text box.
  14.  * <p>
  15.  * @see symantec.itools.awt.Spinner
  16.  * @version 1.0, Nov 26, 1996
  17.  * @author Symantec
  18.  */
  19.  
  20.  
  21. public class ListSpinner
  22.     extends Spinner
  23. {
  24.     //--------------------------------------------------
  25.     // constants
  26.     //--------------------------------------------------
  27.  
  28.  
  29.     //--------------------------------------------------
  30.     // class variables
  31.     //--------------------------------------------------
  32.  
  33.  
  34.     //--------------------------------------------------
  35.     // member variables
  36.     //--------------------------------------------------
  37.  
  38.     /**
  39.      * The list of strings that get displayed in the spinner.
  40.      */
  41.     protected Vector list;
  42.  
  43.  
  44.     //--------------------------------------------------
  45.     // constructors
  46.     //--------------------------------------------------
  47.  
  48.     /**
  49.      * Constructs an empty ListSpinner.
  50.      */
  51.     public ListSpinner()
  52.     {
  53.         list = new Vector();
  54.     }
  55.  
  56.  
  57.     //--------------------------------------------------
  58.     // accessor methods
  59.     //--------------------------------------------------
  60.  
  61.  
  62.     //--------------------------------------------------
  63.     // event methods
  64.     //--------------------------------------------------
  65.  
  66.  
  67.     //--------------------------------------------------
  68.     // class methods
  69.     //--------------------------------------------------
  70.  
  71.  
  72.     //--------------------------------------------------
  73.     // member methods
  74.     //--------------------------------------------------
  75.  
  76.     /**
  77.      * Tells this component that it has been added to a container.
  78.      * This is a standard Java AWT method which gets called by the AWT when 
  79.      * this component is added to a container. Typically, it is used to 
  80.      * create this component's peer.
  81.      * Here it's used to get the length of the largest string in the list.
  82.      *
  83.      * @see java.awt.Container#removeNotify
  84.      */
  85.     public void addNotify()
  86.     {
  87.         if (list.size() > 0) {
  88.  
  89.             for (Enumeration e = list.elements(); e.hasMoreElements();)
  90.             {
  91.                 textWidth = Math.max(textWidth, ((String)e.nextElement()).length());
  92.             }
  93.  
  94.             text = (String)list.elementAt(current);
  95.             setMax(list.size() - 1);
  96.         }
  97.  
  98.         super.addNotify();
  99.     }
  100.  
  101.     /**
  102.      * Add a String to the list.
  103.      * @param s the String to be appended to the list
  104.      * @see #setListItems
  105.      */
  106.     public void addItem(String s)
  107.     {
  108.         list.addElement(s);
  109.         textWidth = Math.max(textWidth, s.length());
  110.         setMax(list.size() - 1);
  111.     }
  112.  
  113.     /**
  114.      * Gets the currently selected String from the list.
  115.      * @return the String currently visible in the Spinner
  116.      */
  117.     public String getCurrentText()
  118.     {
  119.         return list.size() > 0 ? (String)list.elementAt(current) : null;
  120.     }
  121.  
  122.     /**
  123.      * Adds the given string array to the list.
  124.      * @param items items to add to the list
  125.      * @see #getListItems
  126.      */
  127.     public void setListItems(String[] items)
  128.     {
  129.         for (int i = 0; i < items.length; ++i)
  130.         {
  131.             addItem(items[i]);
  132.         }
  133.         updateText();
  134.     }
  135.  
  136.     /**
  137.      * Returns the current list as a array of Strings.
  138.      * @return the current list
  139.      * @see #setListItems
  140.      */
  141.     public String[] getListItems()
  142.     {
  143.         int len = list.size();
  144.         String[] items = new String[len];
  145.         for (int i = 0; i < len; ++i)
  146.         {
  147.             items[i] = (String)list.elementAt(i);
  148.         }
  149.         return items;
  150.     }
  151. }